package oa.dao.impl;

import java.util.List;

import oa.base.BaseDao;
import oa.dao.ApplicationDao;
import oa.entity.Application;
import oa.exception.DaoException;

public class ApplicationDaoImpl extends BaseDao<Application> implements ApplicationDao {

	@Override
	public List<Application> selectAllApplication() throws DaoException {
		return executeQuery("SELECT * FROM application");
	}

	@Override
	public List<Application> selectApplication(int id, String type, int status) throws DaoException {
		String sql = null;
		Object obj = null;
		if (id != 0) {
			sql = "SELECT * FROM application WHERE application_id = ?";
			obj = id;
		} else if (!type.equals("")) {
			sql = "SELECT * FROM application WHERE application_type = ?";
			obj = type;
		} else if (status != -1) {
			sql = "SELECT * FROM application WHERE application_status = ?";
			obj = status;
		} else {
			sql = "SELECT * FROM application";
		}
		return executeQuery(sql, obj);
	}

	@Override
	public boolean deleteApplication(int id) throws DaoException {
		return executeUpdate("DELETE FROM application WHERE application_id = ?", id) > 0;
	}

	@Override
	public boolean upadteApplication(Application application) throws DaoException {
		if (application.getApplicationRefuse() == null) {
			return executeUpdate(
					"UPDATE `oa`.`application` SET `application_status`=?, `application_times`=? WHERE (`application_id`=?)",
					application.getApplicationStatus(), application.getApplicationTimes(),
					application.getApplicationId()) > 0;

		} else {

			return executeUpdate(
					"UPDATE `oa`.`application` SET `application_status`=?, `application_times`=?, `application_refuse`=? WHERE (`application_id`=?)",
					application.getApplicationStatus(), application.getApplicationTimes(),
					application.getApplicationRefuse(), application.getApplicationId()) > 0;
		}
	}

	@Override
	public boolean addApplication(Application application) throws DaoException {
		return executeUpdate(
				"INSERT INTO `oa`.`application` ( `application_type`, `application_title`, `application_reason`, `application_status`, `application_time`, `application_times`, `application_refuse`, `user_id`, `company_id`) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?)",
				application.getApplicationType(), application.getApplicationTitle(), application.getApplicationReason(),
				application.getApplicationStatus(), application.getApplicationTime(), application.getApplicationTimes(),
				application.getApplicationRefuse(), application.getUserId(), application.getCompanyId()) > 0;
	}

	@Override
	public List<Application> selectApplications( String status, int companyid, Integer pageNo, Integer row)
			throws DaoException {
		if(pageNo == 0 && row == 0) {
			return executeQuery("SELECT * FROM application");
		}else if(companyid==0&&status !=null){
			return executeQuery("SELECT * FROM application  WHERE  application_status = ?  LIMIT ?,?",status,(pageNo-1)*row,row);
		}else if(companyid!=0&&status !=null){
			return executeQuery("SELECT * FROM application  WHERE  application_status = ? and company_id = ?   LIMIT ?,?",status,companyid,(pageNo-1)*row,row);
		}else if(companyid!=0){
			return executeQuery("SELECT * FROM application  WHERE   company_id = ?  LIMIT ?,?",companyid,(pageNo-1)*row,row);
		}
		else if( status==null && companyid==0){
			return executeQuery("SELECT * FROM application  LIMIT ?,?",(pageNo-1)*row,row);
		}
		return null;
	}

	@Override
	public int appcount(String status, int companyid,int userid) throws DaoException {
		if(status!=null&&companyid !=0){
			return executeIntQuery("SELECT COUNT(*) FROM application WHERE application_status = ? and company_id = ?",status,companyid);
		}else if(status!=null&&companyid==0){
			return executeIntQuery("SELECT COUNT(*) FROM application WHERE application_status = ?",companyid);
		
		}else if(status==null&&companyid ==0 &&userid!=0) {
			return executeIntQuery("SELECT COUNT(*) FROM `application` WHERE `user_id` = ?",userid);
			
		}else if(status==null&&companyid!=0&&userid==0){
			return executeIntQuery("SELECT COUNT(*) FROM `application` WHERE `company_id`=?", companyid);
		}
		else if (status==null&&companyid == 0&&userid==0) {
			return executeIntQuery("SELECT COUNT(*) FROM `application`");
		} 
		
		
		return 0;
		
		
	}
    //默认获取根据当前登录的员工只能查看自己的申请
	@Override
	public List<Application> selectApplicationusercompany(int userid, Integer pageNo, Integer row) throws DaoException {
		return executeQuery("SELECT * FROM application  WHERE user_id = ?   LIMIT ?,?",userid,(pageNo-1)*row,row);
	}
}
